<?php
$index = 1;

$name_label = elgg_echo('submana:addprojects:name');

$descp_label = elgg_echo('Mô tả đề tài');

$max_group_label = elgg_echo('submana:addprojects:maxgroup');
$total_number_project = 1;
$max_student = 2;
$min_student = 2;
// check if reuse data
if (isset ($vars ['reused']) && $vars['extra'] == 'new') {
    // get group_project
    $group_project = $vars ['reused'];
    $semester_reused = $vars ['semester_reused'];
    $academic_year_reused = $vars ['academic_year_reused'];

    // get list project
    $metadata_options = array(
        array(
            'name' => 'group_project',
            'value' => $group_project
        ),
        array(
            'name' => 'semester',
            'value' => $semester_reused
        ),
        array(
            'name' => 'academic_year',
            'value' => $academic_year_reused
        ),
    );

    $options = array(
        'type' => "object",
        'subtype' => 'project',
        'container_guid' => $vars ['group_guid'],
        'metadata_name_value_pairs' => $metadata_options
    );

    $projects = elgg_get_entities_from_metadata($options);
    $content = "<div id = 'addprojects_wrapcontent'>";
    foreach ($projects as $project) {

        if ($project->parent_project) continue;

        $content .= '<div style="border:solid 1px rgb(181, 190, 190); padding:20px;">';
        $content .= "<div>";
        $content .= '<label for="name">' . $name_label . '</label>';

        $name_input = elgg_view('input/text', array(
            'name' => 'name' . $index,
            'class' => "required",
            'value' => $project->title
        ));
        $content .= $name_input;
        $content .= "</div>";

        $content .= "<div>";
        $content .= '<label for="name">' . $descp_label . '</label>';

        $descp_input = elgg_view('input/plaintext', array(
            'name' => 'descp' . $index,
            'class' => "required",
            'value' => $project->description
        ));
        $content .= $descp_input;
        $content .= "</div>";

        $content .= '<div style="width:60%; border:">';
        $content .= '<label for = "max-group">' . $max_group_label . '</label>';

        $content .= '<input type="number" value='.$project->max_group.' name="max_group' . $index . '" min="1">';
        $content .= "</div>";

        $content .= "</div> <br/>";

        $index++;
        $min_student = $project->min_student;
        $max_student = $project->max_student;
    }
    $total_number_project = $index;
    $content .= '</div>';
    // gen HTML
}

// semester
$group_project = $vars ['group_project'];
$semester = $vars ['semester'];
$academic_year = $vars ['academic_year'];

$groupproject_label = elgg_echo('submana:addprojects:groupproject');
$groupproject_labeltext = elgg_echo("Sử dụng lại mã lớp đã có");

$groupproject_inputtext = elgg_view('input/text', array(
    'name' => "group_project",
    'class' => "required",
    'id' => "theinput",
));

if (isset ($vars ['reused'])) {
    // set name + semester + year
    $groupproject_inputtext = elgg_view('input/text', array(
        'name' => "group_project",
        'class' => "required",
        'id' => "theinput",
        'value' => $vars ['reused']
    ));
}

$ar_groupproject = explode(",", $group_project);
$ar_semesters = explode(",", $semester);
$ar_academic_years = explode(",", $academic_year);

$groupproject_input = "<option></option>";
for ($i = 0; $i < count($ar_groupproject); $i++) {
    if ($ar_groupproject[$i] != "") {
        $groupproject_input .= "<option value='$ar_groupproject[$i]'>$ar_groupproject[$i]" . "/" . $ar_semesters[$i] . "/" . $ar_academic_years[$i] . "</option>";
    }
}

$date = new DateTime('now');
$date = date_format($date, 'd/m/Y');
// add deadline form
$deadline_label = elgg_echo('submana:deadline');
$deadline_input = elgg_view('input/date', array(
    'name' => 'deadline',
    'class' => "required",
    'value' => $date
));
// add maximum student
$max_student_label = elgg_echo('Số sinh viên tối đa/nhóm');
$min_student_label = elgg_echo('Số sinh viên tối tối thiểu/nhóm');
$auto_approve = elgg_view("input/checkbox", array(
	'name' => "autoapprove",
	'text' => "tự động chấp nhận khi đủ điều kiện đăng kí"
));

// $total_label = elgg_echo ( 'submana:addprojects:total' );
$total_input = elgg_view('input/hidden', array(
    'name' => 'total',
    'id' => 'addprojects_total',
    'value' => $total_number_project,
));

$add_button = elgg_view('input/button', array(
    'name' => 'action',
    'value' => elgg_echo('submana:addprojects:addproject'),
    'class' => 'elgg-button-action',
    'onclick' => "next()"
));

$reuse_link = elgg_view('output/url', array(
    'name' => 'action',
    'id' => 'reused_div',
    'style' => 'display:none',
    'text' => elgg_echo("Sử dụng lại danh sách đề tài của lớp có mã"),
    'href' => "",
));

$name_input = elgg_view('input/text', array(
    'name' => 'name1',
    'class' => "required"
));

$descp_input = elgg_view('input/plaintext', array(
    'name' => 'descp1',
    'class' => "required"
));

$max_group_input = elgg_view('input/text', array(
    'name' => 'max_group1',
    'value' => 1,
    'class' => "required"
));

$hidden_input = elgg_view('input/hidden', array(
    'name' => 'group_guid',
    'value' => $vars ['group_guid']
));

$save_button = elgg_view('input/submit', array(
    'value' => elgg_echo('Lưu danh sách đề tài'),
));
$reset_button = elgg_view('input/reset', array(
    'value' => elgg_echo('submana:addprojects:reset')
));

$back_button = elgg_view('output/url', array(
    'name' => 'action',
    'class' => 'elgg-button elgg-button-cancel',
    'text' => elgg_echo("Quay về"),
    'href' => "submana/listpro/" . $vars['group_guid'],
));

$semester_label = elgg_echo('Học kỳ');
$academic_year_label = elgg_echo('Năm học');

$semester_input = '<select id="semester_input" name="semester" type="dropdown" class="elgg-input-dropdown">';
if (isset ($vars ['reused']) && $vars['extra'] == 'existed') {
    // set name + semester + year
    $semester_input .= "<option selected='selected' value='" . $vars['semester_reused'] . "'>" . $vars['semester_reused'] . "</option>";
} else {

    $semester_input .= "<option value='HK1'>HK1</option>";
    $semester_input .= "<option value='HK2'>HK2</option>";
    $semester_input .= "<option value='HK Hè'>HK Hè</option>";
}
$semester_input .= '</select>';


$date = new DateTime('now');
$date = date_format($date, 'Y');
$this_year = (int)$date;

$academic_year_input = '<select id="academic_year_input" name="academic_year" type="dropdown" class="elgg-input-dropdown">';
if (isset ($vars ['reused']) && $vars['extra'] == 'existed') {
    // set name + semester + year
    $academic_year_input .= "<option selected='selected' value='" . $vars['academic_year_reused'] . "'>" . $vars['academic_year_reused'] . "</option>";

} else {
    $academic_year_input .= "<option value='" . ($this_year - 1) . " - " . ($this_year) . "'>" . ($this_year - 1) . " - " . ($this_year) . "</option>";
    $academic_year_input .= "<option value='" . ($this_year) . " - " . ($this_year + 1) . "'>" . ($this_year) . " - " . ($this_year + 1) . "</option>";
    $academic_year_input .= "<option value='" . ($this_year + 1) . " - " . ($this_year + 2) . "'>" . ($this_year + 1) . " - " . ($this_year + 2) . "</option>";
    $academic_year_input .= "<option value='" . ($this_year + 2) . " - " . ($this_year + 3) . "'>" . ($this_year + 2) . " - " . ($this_year + 3) . "</option>";
    $academic_year_input .= "<option value='" . ($this_year + 3) . " - " . ($this_year + 4) . "'>" . ($this_year + 3) . " - " . ($this_year + 4) . "</option>";
}
$academic_year_input .= '</select>';


echo <<<___HTML
<div>
	<label for="groupproject">$groupproject_label</label>
	<div class="">$groupproject_inputtext</div>
	<label for="groupproject">$groupproject_labeltext</label>
	<select type="dropdown" class="elgg-input-dropdown" name="thelist" onChange="combo(this, 'theinput')" onMouseOut="comboInit(this, 'theinput')" >
	$groupproject_input
	</select>
	</div>
	<div class="clearboth" />
</select>
</div>
<div>
<label for="semester">$semester_label</label>
$semester_input
</div>

<div>
<label for="academic_year">$academic_year_label</label>
$academic_year_input
</div>

<div class="left" style = "width:40%;">
	<label for="dealine">$deadline_label</label>
	$deadline_input
</div>
<div style = "width:40%;">
	<label for="dealine">$max_student_label</label>
	<input type="number" value=$max_student name="max_student" min="1">
</div>
<div style = "width:40%;">
	<label for="dealine">$min_student_label</label>
	<input type="number" value=$min_student name="min_student" min="1">
</div>
<div>
	<label>$auto_approve &nbsp <b>Tự động chấp nhận khi đủ điều kiện đăng kí<b></label>
<div>
<div class="left" style = "width:25%; margin-left: 50px;">
	<label for="total-project">$total_label</label>
	$total_input
</div>

<div class="clearboth" style="clear:both;" ></div>
<div class="right">$reuse_link</div>
<div class="clearboth" style="clear:both;" ></div> <br/>
___HTML;

//------------- if reuser -> show the reuser items ----------------// 
if (isset ($vars ['reused']) && $vars['extra'] == 'new') {
    echo $content;
} else {
	// echo nomarly
    echo <<<___HTML
<div id = "addprojects_wrapcontent">
	<div style="border:solid 1px rgb(181, 190, 190); padding:20px;">
	<div>
		<label for="name">$name_label</label>
		$name_input
	</div>
	<div>		<label for="desp">$descp_label</label>
		$descp_input
	</div>
	<div style="width:60%; border:">
		<label for = "max-group">$max_group_label</label>
		<input type="number" value=1 name="max_group1" min="1">
	</div>

	</div>

</div>
___HTML;
}

echo <<<___HTML
</br>

<div style="">
	$add_button
</div> <br/> <br/>

<div>
	$hidden_input
	$save_button
	$back_button
</div>

___HTML;

?>


<script type="text/javascript">

    var group_guid = <?php echo $vars['group_guid'];?>;

    var available_projects = <?php if ($index > 1) echo $index - 1; else echo "1"; ?>;

    function next() {

        if (available_projects) {
            {
                available_projects++;
                var strHTML = '<div style="border:solid 1px rgb(181, 190, 190); padding:20px; margin-top: 5px;">' +
                    '<div>' +
                    '<label for="name"><?php echo elgg_echo ( 'submana:addprojects:name' );?></label>' +
                    '<input type="text" value="" name="name' + available_projects + '" class="elgg-input-text">' +
                    '</div>' +
                    '<label for="desp">Mô tả đề tài</label>' +
                    '<textarea rows="10" cols="50" name="descp' + available_projects + '" class="elgg-input-plaintext required"></textarea>' +
                    '<div style="width:60%; border:">' +
                    '<label for="max-group">Số nhóm tối đa thực hiện đề tài này</label>' +
                    '<input type="number" value="1" min="1" name="max_group' + available_projects + '" class="elgg-input-text">' +
                    '</div>' +
                    '</div>';

                $('#addprojects_wrapcontent').append(strHTML);

                $("#addprojects_total").val(available_projects);
            }
        }
    }

    function comboInit(thelist) {
        theinput = document.getElementById(theinput);
        var idx = thelist.selectedIndex;
        var content = thelist.options[idx].value;
        if (theinput.value == "")
            theinput.value = content;
    }

    function combo(thelist, theinput) {
        theinput = document.getElementById(theinput);
        var idx = thelist.selectedIndex;
        var content = thelist.options[idx].value;
        theinput.value = content;

        var reuselink = document.getElementById('reused_div');
        var semester = document.getElementById('semester_input');
        var academic_year = document.getElementById('academic_year_input');

        if (content != "") {
            reuselink.setAttribute('href', 'http://localhost/igs/submana/addlistpro/' + group_guid + '/' +
                thelist.options[idx].text + '/' + 'new');
            reuselink.setAttribute('style', 'display: inline;');

            reuselink.innerHTML = "Sử dụng lại danh sách đề tài của lớp có mã " + thelist.options[idx].text;
        } else {
            reuselink.setAttribute('style', 'display: none;');
        }
    }
</script>
